package com.micro.modual.model.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Objects;
import java.util.stream.Stream;

/**
 * @Desc 优惠券分类
 * @author LevelCoder
 * @version V1.0.0
 * @email LevelCoder@126.com
 * @date 2020/2/8
 */
@Getter
@AllArgsConstructor
public enum CouponCategory {

    FULL_REDUCTION("满减券","001"),
    DISCOUNT("折扣券","002"),
    VERTICAL_REDUCTION("立减券","003")
    ;

    /**
     * 优惠券分类描述
     */
    private String description;

    /**
     * 优惠券分类编码
     */
    private String code;

    public static CouponCategory of(String code){
        //判断code不能为空,如果为空则抛出异常
        Objects.requireNonNull(code);
        //通过Stream进行返回
        //Stream.of(values()) 返回CouponCategory数组
        //.filter(bean -> bean.code.equals(code)) 通过过滤器进行code过滤
        //.findAny() 随便挑选一个
        //.orElseThrow 如果找不到则进行抛出异常 orElseThrow 如果找到则返回值否则抛出异常
        return Stream.of(values())
                .filter(bean -> bean.code.equals(code))
                .findAny()
                .orElseThrow(() -> new IllegalArgumentException(code +"not exists"));
    }
}
